package com.tjetc.dao.impl;

import com.tjetc.dao.AdminDao;
import com.tjetc.domain.Admin;
import com.tjetc.utils.Db;
import com.tjetc.utils.PageInfo;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class AdminDaoImpl implements AdminDao {
    //超级管理员
    @Override
    public Admin login(String username, String password,int rid) throws SQLException {
        return Db.runner0().query(Db.getConnection(),"SELECT a.* FROM admin a\n" +
                        "JOIN admin_role b ON a.id = b.aid\n" +
                        "WHERE a.username = ? \n" +
                        "AND a.password = ? \n" +
                        "AND a.status = 1 \n" +
                        "AND b.rid = ?;",
                new BeanHandler<>(Admin.class),username,password,rid);
    }

    @Override
    public int getCount(String username, String phone) throws SQLException {
        return ((Long)Db.runner0().query("select count(id) from admin where username like ? and phone like ?",
                new ScalarHandler<>(),"%"+username+"%","%"+phone+"%")).intValue();
    }

    @Override
    public List<Admin> list(String username, String phone, PageInfo<Admin> pageInfo) throws SQLException {
        return Db.runner0().query("select * from admin where username like ? and phone like ? limit ?,? ",
                new BeanListHandler<>(Admin.class),"%"+username+"%","%"+phone+"%",
                pageInfo.getStartRecord(),pageInfo.getPageSize());
    }

    @Override
    public int add(Admin admin) throws SQLException {
        return Db.runner().update(Db.getConnection(),"insert into admin(username, password, phone, sex, age, email, avatar)" +
                " VALUES (?,?,?,?,?,?,?)",admin.getUsername(),admin.getPassword(),admin.getPhone(),admin.getSex(),
                admin.getAge(), admin.getEmail(),admin.getAvatar());
    }

    @Override
    public Admin getById(int id) throws SQLException {
        return Db.runner0().query("select * from admin where id = ?",new BeanHandler<>(Admin.class),id);
    }

    @Override
    public int findMaxId() throws SQLException {
        return Db.runner().query(Db.getConnection(),"select max(id) from admin",new ScalarHandler<>());
    }

    @Override
    public Admin getpwdById(int id) throws SQLException {
        return Db.runner0().query("select * from admin where id=?",
                new BeanHandler<>(Admin.class),
                id);
    }

    @Override
    public int updatepwd(Admin admin) throws SQLException {
        return Db.runner0().update("update admin set password =? where id=?",
                admin.getPassword(),admin.getId());
    }

    @Override
    public Admin findByName(String username) throws SQLException {
        return Db.runner0().query("select * from admin  where username=?",
                new BeanHandler<>(Admin.class),username);
    }


    @Override
    public int update(Admin admin) throws SQLException {
        return Db.runner0().update("update admin set username=?,phone=?,sex=?,age=?,email=?,avatar=? where id = ?",
                admin.getUsername(),admin.getPhone(),admin.getSex(),admin.getAge(),
                admin.getEmail(),admin.getAvatar(),admin.getId());
    }

    @Override
    public int updateStatus(int status,int id) throws SQLException {
        return Db.runner0().update("update admin set status = ? where id = ?",status,id);
    }

    @Override
    public int getMaxId() throws SQLException {
        return Db.runner0().query("select max(id) from admin",new ScalarHandler<>());
    }

}
